/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battle.ship.model;

import battle.ship.utils.Util;
import java.io.*;
import java.util.*;
import java.util.logging.*;

/**
 *
 * @author Carlos
 */

public class Game implements Serializable{
    private List<Player> playerList;
    private Integer numberOfPlayers;
    private UUID Id;
    private GameState state;
    
    private static final Logger logger = Logger.getLogger(Game.class.getName());
    
    public Game(Integer numberOfPlayers)
    {
        this.state = GameState.INITIALIZING;
        this.Id = UUID.randomUUID();
        this.numberOfPlayers = numberOfPlayers;
        this.playerList = new ArrayList<Player>(numberOfPlayers);
    }
  
    public Boolean isFull(){
        return this.playerList.size() == this.numberOfPlayers;
    }
    public UUID getId() {
        return this.Id;
    }

    public GameState getState() {
        return this.state;
    }
    
    public Integer getNumberOfPlayers() {
        return this.numberOfPlayers;
    }

    public void setNumberOfPlayers(Integer NumberOfPlayers) {
        this.numberOfPlayers = NumberOfPlayers;
    }

    public void setPlayerList(List<Player> playerList) {
        this.playerList = playerList;
    }

    public List<Player> getPlayerList() {
        return this.playerList;
    }
    
    public Boolean addPalyer(Player player)
    {
        if (this.playerList.contains(player))
            return false;
        this.playerList.add(player);
        return true;
    }
    
    public static Game getGameFromSerializedObject(String base64SerilizedObject)
    {
        try
        {
            return (Game)Util.desserilizeFromString(base64SerilizedObject);
        }
        catch(IOException | ClassNotFoundException ex)
        {
            logger.log(Level.SEVERE, ex.getMessage(), ex);
            return null;
        }
    }
    
    public String getSerializedGame()
    {
        try
        {
            return Util.serializableToString(this);
        }
        catch (IOException ex)
        {
            logger.log(Level.SEVERE, ex.getMessage(), ex);
            return null;
        }
    }
}

